<?php
/**
 * Active.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-2 下午9:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class Active extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'Active',
        'fields' => [
            'Id' => self::VAR_TYPE_KEY,
            'CityId' => self::VAR_TYPE_INT,
            'ShopId' => self::VAR_TYPE_INT,
            'Name' => self::VAR_TYPE_STRING,
            'Cover' => self::VAR_TYPE_STRING,
            'ImgUrl' => self::VAR_TYPE_STRING,
            'Description' => self::VAR_TYPE_STRING,
            'ViewCount' => self::VAR_TYPE_INT,
            'StartTime' => self::VAR_TYPE_STRING,
            'EndTime' => self::VAR_TYPE_STRING,
            'MinPrice' => self::VAR_TYPE_STRING,
            'MaxPrice' => self::VAR_TYPE_STRING,
            'PriceType' => self::VAR_TYPE_INT,
            'Rules' => self::VAR_TYPE_STRING,
            'MaxOffer' => self::VAR_TYPE_INT,
            'Sort' => self::VAR_TYPE_INT,
            'Status' => self::VAR_TYPE_INT,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
            'RewardDays' => self::VAR_TYPE_INT,
            'RewardType' => self::VAR_TYPE_INT,
            'Amount' => self::VAR_TYPE_INT,
            'CouponId' => self::VAR_TYPE_INT,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'Active:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const STATUS_ONGOING = 1;
    const STATUS_FINISH = 2;

    /**
     * 获取城市活动列表
     *
     * @param       $cityid
     * @param int   $offset
     * @param int   $limit
     * @param int   $status
     * @param array $active_ids
     * @return array
     */
    public static function getListByCity(
        $cityid,
        $offset = 0,
        $limit = 20,
        $status = self::STATUS_ONGOING,
        $active_ids = []
    ) {
        $where = [
            'CityId' => $cityid,
        ];
        if (!empty($active_ids)) {
            $where['Id IN'] = $active_ids;
        }
        if ($status > 0) {
            $where['Status'] = $status;
        }

        return self::getList($where, $offset, $limit, '*', 'Sort ASC');
    }

    /**
     * 获取店铺最新的活动
     *
     * @param $shopid
     * @return array
     */
    public static function getLastActive($shopid)
    {
        $where = [
            'ShopId' => $shopid,
            'Status' => self::STATUS_ONGOING
        ];
        $list_info = parent::getList($where, 0, 1);

        return !empty($list_info['data'][0]) ? $list_info['data'][0] : [];
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}